package com.dwaynema.controller;

import com.alibaba.druid.sql.visitor.functions.Bin;
import com.dwaynema.common.RestResult;
import com.dwaynema.common.RestResultEnum;
import com.dwaynema.common.Result;
import com.dwaynema.dto.UserDto;
import com.dwaynema.entity.SysUser;
import com.dwaynema.service.UserService;
import com.dwaynema.util.MD5Util;
import com.dwaynema.util.RestResultUtil;
import com.dwaynema.util.TokenUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    Result resultMsg=null;
    @Autowired
    UserService userService;
    @Autowired
    UserDto userDto;

    @Autowired
    TokenUtil tokenUtil;

    @CrossOrigin
    @PostMapping("/login")
    @ResponseBody
    public RestResult userLogin(@RequestBody @Valid SysUser sysUser, BindingResult result, HttpServletResponse response) {
        if(result.hasErrors()){
            resultMsg=new Result();
            resultMsg.setDefaultMessage("参数错误");
            RestResultUtil.result(RestResultEnum.ERROR,"参数错误",resultMsg);
        }
        String password=sysUser.getPassword();
        sysUser.setPassword(MD5Util.code(password));
        userDto=userService.userLogin(sysUser);
        if(userDto==null){
            resultMsg=new Result();
            resultMsg.setDefaultMessage("用户或密码错误");
            return RestResultUtil.result(RestResultEnum.ERROR,"登录失败",resultMsg);
        }
        String jwt= tokenUtil.generateToken(sysUser.getUsername());
        response.setHeader("Authorization", jwt);
        response.setHeader("Access-control-Expose-Headers", "Authorization");
        return RestResultUtil.result(RestResultEnum.SUCCESS,"请求成功",userDto);
    }

    @PostMapping("/register")
    @ResponseBody
    @RequiresAuthentication
    public RestResult userRegister(@RequestBody @Valid SysUser sysUser,BindingResult result){
        Integer register;
        if(result.hasErrors()){
            resultMsg=new Result();
            resultMsg.setDefaultMessage("参数错误");
            RestResultUtil.result(RestResultEnum.ERROR,"参数错误",resultMsg);
        }
        String password=sysUser.getPassword();
        sysUser.setPassword(MD5Util.code(password));
        register=userService.userRegister(sysUser);
        if(register>0){
            return RestResultUtil.result(RestResultEnum.SUCCESS,"创建成功","");
        }else {
            return RestResultUtil.result(RestResultEnum.SUCCESS,"创建失败","");
        }
    }
}
